package demo.test.bean;

import com.just_bean.core.bean.BaseBean;
import com.just_bean.core.exception.BeanInitException;
import demo.test.anno.Component;

public class ComponentBean extends BaseBean<Component> {
    // 单例bean
    public static final String SINGLE = "";
    // 普通bean
    public static final String NORMAL = "normal";

    private boolean isSingle;
    private String name;
    private Object instance;

    public ComponentBean(Class clz, Component a) throws Exception {
        super(clz, a);
        this.isSingle = SINGLE.equals(a.scope());
        String diName = a.name();
        this.name = "".equals(diName) ? this.clzFieldName(clz.getSimpleName()) : diName;
    }

    private String clzFieldName(String clzName) {
        return Character.toLowerCase(clzName.charAt(0)) + clzName.substring(1);
    }

    @Override
    public boolean check(Object... cond) {
        return this.name.equals(cond[0]);
    }

    @Override
    public Object get() throws BeanInitException {
        if (isSingle) {
            return instance != null ? instance : (instance = this.create());
        } else {
            return this.create();
        }
    }
}
